<?php
/**
 * Created by PhpStorm.
 * User: 54946
 * Date: 2017/8/8
 * Time: 22:26
 */
namespace App\Repositories;

use App\Http\Model\User;
use Illuminate\Support\Facades\Crypt;

class UserRepository {

    use BaseRepository;
    protected $model;

    /**
     * UserRepository constructor.
     * @param User $user
     */
    public function __construct(User $user)
    {
        $this->model=$user;
    }

    /**
     * 处理登录请求
     * @param $input
     * @return array
     */
    public function handleLogin($input)
    {

        $name=$input['userName'];
        $password=$input['password'];

        $result=$this->model->where('name',$name)->first();
        if($result){
            if(Crypt:: decrypt($result->password)==$password){
                if($result->status!='1'){
                    return array('status_code'=>500,'message'=>'账号异常');
                }else{
                    return array('status_code'=>200,'message'=>'登录成功');
                }

            }else{
                return array('status_code'=>500,'message'=>'密码错误');
            }

        }else{
            return array('status_code'=>500,'message'=>'用户名错误');
        }

    }

    /**
     * 通过保存在session的用户名，判断是否为超级管理员
     * @return \Illuminate\Database\Eloquent\Model|null|static
     */
    public function getId()
    {
        return $this->model->where('name',session(config('constants.SESSION_NAME')))->first();
    }

}